import { Parameter } from "./Parameter";
import { must } from "../../core/logic";
import { Engine } from "../../Engine";
class Texture extends Parameter {
  /**
   * 
   * @param {*} opts 
   * @param {GPUBufferUsage} opts.usage 
   * @param {GPUShaderStage} opts.visibility
   * @param {String} opts.sampleType "float"(浮点)|"unfilterable"(非过滤)|"depth"(深度)|"sint"(有符号整型)|"uint"(无符号整型)
   * @param {String} opts.format
   * @param {String} opts.dimension 
   * @param {Boolean} opts.multisampled 
   * @param {Blob|GPUTexture} opts.data
   */
  constructor(opts = {}) {
    super(opts);
    //运行参数
    this.gpu = {
      buffer: null,
      layout: null,
      entry: null
    }
    //布局部分
    this.usage = must(opts.usage);
    this.visibility = must(opts.visibility);
    this.sampleType = must(opts.sampleType);
    this.format = must(opts.format);
    this.dimension = must(opts.dimension);
    this.multisampled = must(opts.multisampled);
    this.updateLayout();
    //数据部分
    this.buffer(opts.data);
  }
  get cName () {
    return "Texture";
  }
  set usage (v) {
    this._usage = v;
    return this;
  }
  get usage () {
    return this._usage;
  }
  set visibility (v) {
    this._visibility = v;
    return this;
  }
  get visibility () {
    return this._visibility;
  }
  set sampleType (v) {
    this._sampleType = v;
    return this;
  }
  get sampleType () {
    return this._sampleType;
  }
  set format (v) {
    this._format = v;
    return this;
  }
  get format () {
    return this._format;
  }
  set dimension (v) {
    this._dimension = v;
    return this;
  }
  get dimension () {
    return this._dimension;
  }
  set multisampled (v) {
    this._multisampled = v;
    return this;
  }
  get multisampled () {
    return this._multisampled;
  }
  updateLayout () {
    this.gpu.layout = {
      binding: this.binding,
      visibility: this.visibility,
      texture: {
        sampleType: this.sampleType,
        viewDimension: this.dimension,
        multisampled: this.multisampled
      }
    }
    if (this.gpu.buffer) {
      this.gpu.buffer.destroy();
      this.gpu.buffer = null;
    }
    return this;
  }
  buffer (data) {
    if (!data) {
      return;
    }
    this.update(data);
  }
  /**
   * 将cpu中的数据拷贝到gpu
   * @method
   */
  update (data) {                                                
    if (this.gpu.buffer) {
      Engine.instance.register.systemManager.get("rendersystem").destroyTask.push(this.gpu.buffer);
    }
    this.gpu.buffer = data;
    this.gpu.buffer.view = this.gpu.buffer.createView({
      format: this.format,
      dimension: this.dimension
    })
    this.gpu.entry = {
      binding: this.binding,
      resource: this.gpu.buffer.view
    }
  }

  destroy () {
    if (this.watchers.length<=0) {
      if (this.gpu && this.gpu.buffer) {
          this.gpu.buffer.destroy();
          this.gpu = null;
          if(this.id){
            Engine.instance.res.textureLoader.textureMap.delete(this.id)
          }
      }
      return true;
    }
    return false;
  }
}
export { Texture }